package com.djf.djfcs.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ配置类
 * 定义数据同步相关的队列、交换机和绑定关系
 */
@Configuration
public class RabbitMQConfig {

    // 交换机名称
    public static final String DATA_SYNC_EXCHANGE = "data.sync.exchange";
    
    // 队列名称
    public static final String ORDER_SYNC_QUEUE = "order.sync.queue";
    public static final String PRODUCT_SYNC_QUEUE = "product.sync.queue";
    
    // 路由键
    public static final String ORDER_ROUTING_KEY = "order.sync";
    public static final String PRODUCT_ROUTING_KEY = "product.sync";
    
    // 死信队列相关
    public static final String DLX_EXCHANGE = "data.sync.dlx.exchange";
    public static final String ORDER_DLQ = "order.sync.dlq";
    public static final String PRODUCT_DLQ = "product.sync.dlq";

    /**
     * 数据同步主交换机
     */
    @Bean
    public TopicExchange dataSyncExchange() {
        return new TopicExchange(DATA_SYNC_EXCHANGE, true, false);
    }

    /**
     * 死信交换机
     */
    @Bean
    public DirectExchange dlxExchange() {
        return new DirectExchange(DLX_EXCHANGE, true, false);
    }

    /**
     * 订单同步队列
     */
    @Bean
    public Queue orderSyncQueue() {
        return QueueBuilder.durable(ORDER_SYNC_QUEUE)
                .withArgument("x-dead-letter-exchange", DLX_EXCHANGE)
                .withArgument("x-dead-letter-routing-key", ORDER_DLQ)
                .withArgument("x-message-ttl", 300000) // 5分钟TTL
                .build();
    }

    /**
     * 商品同步队列
     */
    @Bean
    public Queue productSyncQueue() {
        return QueueBuilder.durable(PRODUCT_SYNC_QUEUE)
                .withArgument("x-dead-letter-exchange", DLX_EXCHANGE)
                .withArgument("x-dead-letter-routing-key", PRODUCT_DLQ)
                .withArgument("x-message-ttl", 300000) // 5分钟TTL
                .build();
    }

    /**
     * 订单死信队列
     */
    @Bean
    public Queue orderDlq() {
        return QueueBuilder.durable(ORDER_DLQ).build();
    }

    /**
     * 商品死信队列
     */
    @Bean
    public Queue productDlq() {
        return QueueBuilder.durable(PRODUCT_DLQ).build();
    }

    /**
     * 订单队列绑定
     */
    @Bean
    public Binding orderSyncBinding() {
        return BindingBuilder.bind(orderSyncQueue())
                .to(dataSyncExchange())
                .with(ORDER_ROUTING_KEY);
    }

    /**
     * 商品队列绑定
     */
    @Bean
    public Binding productSyncBinding() {
        return BindingBuilder.bind(productSyncQueue())
                .to(dataSyncExchange())
                .with(PRODUCT_ROUTING_KEY);
    }

    /**
     * 订单死信队列绑定
     */
    @Bean
    public Binding orderDlqBinding() {
        return BindingBuilder.bind(orderDlq())
                .to(dlxExchange())
                .with(ORDER_DLQ);
    }

    /**
     * 商品死信队列绑定
     */
    @Bean
    public Binding productDlqBinding() {
        return BindingBuilder.bind(productDlq())
                .to(dlxExchange())
                .with(PRODUCT_DLQ);
    }
}